<?php

class M_school extends CI_Model{

	public function get_school(){
		$query = $this->db->get('school');
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	public function update_school($name, $address, $email, $telephone){
		$data = array(
			'name' => $name,
			'address' => $address,
			'email' => $email,
			'telephone' => $telephone,
			'updated' => NOW
		);
		
		$this->db->where('id', 1);
		if($this->db->update('school', $data))
			return true;
		else
			return false;
	}
	
	public function get_announcements(){
		
		
	}
	
	public function get_calendar_events(){
		
	}
	
	public function get_school_year($id = FALSE){
		if($id == FALSE){
			$query = $this->db->select(array('sy_id','sy_title','sy_desc','sy_year','current_year'))->get('school_year');
			return $query->num_rows() > 0 ? $query->result() : FALSE ;
		}else{
			$query = $this->db->select(array('sy_id','sy_title','sy_desc','sy_year','current_year'))->where('sy_id',$id)->get('school_year');
			return $query->num_rows() > 0 ? $query->result() : FALSE ;
		}
	}
	
	public function update_current_schoolyear($id,$data){
		if($this->db->where('sy_id',$id)->set('current_year',0)->update('school_year')){
			if($this->db->where($data)->set('current_year',1)->update('school_year'))
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
	
	
	public function add_school_year($data,$current = FALSE){
		if($current == FALSE){
			$this->db->insert('school_year',$data);
			return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		}else{
			$this->db->set('current_year',0)->where('current_year',1)->update('school_year');
			if($this->db->affected_rows() > 0){
				$data['current_year'] = 1;
				$this->db->insert('school_year',$data);
				return $this->db->affected_rows() > 0 ? TRUE : FALSE;
			}else{
				return FALSE;
			}
		}
	}
	
	public function update_school_year($data,$id){
		$this->db->set($data)->where('sy_id',$id)->update('school_year');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
		
	}
	
	public function delete_school_year($id){
		return $this->db->where('sy_id',$id)->delete('school_year') ? TRUE : FALSE;
	}
	
	public function get_current_sy(){
		$query = $this->db->where('current_year',1)->get('school_year');
		return $query->num_rows() > 0 ? $query->row() : FALSE ;
	}

}

?>